package com.kx.smartdevice.controller;

import com.kx.smartdevice.constants.ResultCode;
import com.kx.smartdevice.dto.R;
import com.kx.smartdevice.exceptions.MyBaseErrorException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName ExceptionHandlerController
 * @Author Sun
 * @Date 2021/5/26 18:46
 */
@ControllerAdvice //切面增强类
public class ExceptionHandlerController {

    /**
     * 捕获项目中自定义异常
     * @param exception
     * @return
     */
    @ExceptionHandler(MyBaseErrorException.class)
    @ResponseBody
    public R handlerException(MyBaseErrorException exception){
        int code = exception.getCode();
        String message = exception.getMessage();
        return R.SetError(null, message, code);
    }

    /**
     * 未知异常错误
     * @param exception
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R handlerException(Exception exception){
       exception.printStackTrace();
        return R.SetError(null, "网络故障，请重试", ResultCode.ERROR);
    }

}
